package com.example.doer.reading.AppAdapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.doer.reading.AppActivities.ThemeActivity;
import com.example.doer.reading.AppActivities.TopicActivity;
import com.example.doer.reading.AppActivities.TopicWithReplyActivity;
import com.example.doer.reading.AppDataModel.Topic;
import com.example.doer.reading.R;

import java.util.List;


/**
 * Created by 严旭江 on 2017/7/11.
 */

public class MyTopicAdapter extends BaseAdapter {

    List<Topic> topicList;
    Context context;
    int resID;

    public MyTopicAdapter(Context theContext, List<Topic> theList, int theResID) {
        this.context = theContext;
        this.topicList = theList;
        this.resID = theResID;
    }

    @Override
    public int getCount() {
        return topicList.size();
    }

    @Override
    public Topic getItem(int position) {
        return topicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //在TopicFragment中显示每个话题view
        View view = LayoutInflater.from(context).inflate(resID, null);
        //获取在位置position的Topic对象的引用
        final Topic topic = getItem(position);
        //获取每个View的各个控件的引用
        //Theme的Logo
        ImageView topicImageView = (ImageView)view.findViewById(R.id.topicImageView);
        //Theme的类别
        final TextView catagory = (TextView) view.findViewById(R.id.topicCatagory);
        //问题名字
        TextView topicName = (TextView) view.findViewById(R.id.topicName);
        //问题的内容
        TextView topicReplyContent = (TextView) view.findViewById(R.id.topicContent);
        //点赞数目
        TextView replyLikeCount = (TextView) view.findViewById(R.id.topicAgreeNumber);
        //回复数目
        TextView replyCommentCount = (TextView) view.findViewById(R.id.topicCommentNumber);

        /**
         * 从服务器获取Img
         */
        topicImageView.setImageResource(R.drawable.user_icon_test);
        //设置话题名字
        catagory.setText(topic.getTopicCatagory());
        topicName.setText(topic.getTopicName());
        /**
         * 根据Topic是否含Reply判断
         */
        if(topic.getaTopicReply()!=null) {
            topicReplyContent.setText(topic.getaTopicReply().getReplyContent());
            replyLikeCount.setText("点赞数:"+topic.getaTopicReply().getFavoCount());
            replyCommentCount.setText("评论数："+topic.getReplyCount());
        }
        else {
            topicReplyContent.setVisibility(View.GONE);
            replyLikeCount.setVisibility(View.GONE);
            replyCommentCount.setVisibility(View.GONE);
        }
        /**
         * 点击话题Theme   -->点击事件
         */
        ((TextView)view.findViewById(R.id.topicCatagory)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取Theme
                Intent intent = new Intent(v.getContext(), ThemeActivity.class);
                //传递Theme名字
                //intent.putExtra("themeName",catagory.getText().toString());
                Bundle bundle = new Bundle();
                bundle.putString("themeName",catagory.getText().toString());
                intent.putExtras(bundle);
                v.getContext().startActivity(intent);
            }
        });
        /**
         * 点击Question名进行页面转换
         */
        ((TextView)view.findViewById(R.id.topicName)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用topic
                Intent intent = new Intent(v.getContext(),TopicWithReplyActivity.class);
                intent.putExtra("topicObjWithReply",topic);
                v.getContext().startActivity(intent);
            }
        });
        /**
         * 点击Answer内容进行页面转换
         */
        ((TextView)view.findViewById(R.id.topicContent)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用topic
                Intent intent = new Intent(v.getContext(),TopicActivity.class);
                intent.putExtra("topicReplyObj",topic);
                v.getContext().startActivity(intent);
            }
        });
        return view;
    }
    public void addTopicToRefresh(Topic theTopic){
        topicList.add(theTopic);
        notifyDataSetChanged();
    }
}